package edu.uoc.tdp.client.pantalles;

import javax.swing.JPanel;
import java.awt.Frame;
import javax.swing.JDialog;
import edu.uoc.tdp.client.comu.Util;
import edu.uoc.tdp.client.gestors.GestorLogin;
import javax.swing.WindowConstants;
import javax.swing.JLabel;
import java.awt.Rectangle;
import javax.swing.SwingConstants;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JPasswordField;

public class PantallaLogin extends JDialog {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JLabel jLabelNomUsuari = null;
	private JLabel jLabelContrasenya = null;
	private JTextField jTextFieldUsuari = null;
	private JButton jButtonAcceptar = null;
	private JButton jButtonCancellar = null;
	private GestorLogin gestor;
	private JPasswordField jPasswordField = null;
	
	public PantallaLogin(Frame owner) {
		super(owner);
		this.gestor = new GestorLogin(this);
		initialize();
	}

	private void initialize() {
		this.setSize(285, 175);
		this.setModal(true);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		this.setTitle(Util.GetEtiqueta("Login.TitolFinestra"));
		this.setContentPane(getJContentPane());
		this.rootPane.setDefaultButton(jButtonAcceptar);
		this.setVisible(true);
		this.addWindowListener(new java.awt.event.WindowAdapter() {
			public void windowClosing(java.awt.event.WindowEvent e) {
				return;
			}
		});
	}


	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jLabelContrasenya = new JLabel();
			jLabelContrasenya.setBounds(new Rectangle(15, 45, 87, 23));
			jLabelContrasenya.setHorizontalAlignment(SwingConstants.RIGHT);
			jLabelContrasenya.setText(Util.GetEtiqueta("Login.Contrasenya"));
			jLabelNomUsuari = new JLabel();
			jLabelNomUsuari.setBounds(new Rectangle(15, 15, 86, 21));
			jLabelNomUsuari.setHorizontalAlignment(SwingConstants.RIGHT);
			jLabelNomUsuari.setText(Util.GetEtiqueta("Login.Usuari"));
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(jLabelNomUsuari, null);
			jContentPane.add(jLabelContrasenya, null);
			jContentPane.add(getJTextFieldUsuari(), null);
			jContentPane.add(getJButtonAcceptar(), null);
			jContentPane.add(getJButtonCancellar(), null);
			jContentPane.add(getJPasswordField(), null);
		}
		return jContentPane;
	}

	private JTextField getJTextFieldUsuari() {
		if (jTextFieldUsuari == null) {
			jTextFieldUsuari = new JTextField();
			jTextFieldUsuari.setBounds(new Rectangle(120, 15, 136, 22));
		}
		return jTextFieldUsuari;
	}


	private JButton getJButtonAcceptar() {
		if (jButtonAcceptar == null) {
			jButtonAcceptar = new JButton();
			jButtonAcceptar.setBounds(new Rectangle(30, 90, 108, 34));
			jButtonAcceptar.setText(Util.GetEtiqueta("Boto.Acceptar"));
			jButtonAcceptar.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					gestor.BotoAcceptar();			
				}
			});
		}
		return jButtonAcceptar;
	}


	private JButton getJButtonCancellar() {
		if (jButtonCancellar == null) {
			jButtonCancellar = new JButton();
			jButtonCancellar.setBounds(new Rectangle(150, 90, 109, 33));
			jButtonCancellar.setText(Util.GetEtiqueta("Boto.Cancellar"));
			jButtonCancellar.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					gestor.BotoCancellar();
					TancarFinestra();
				}
			});
		}
		return jButtonCancellar;
	}


	private JPasswordField getJPasswordField() {
		if (jPasswordField == null) {
			jPasswordField = new JPasswordField();
			jPasswordField.setBounds(new Rectangle(120, 46, 136, 23));
		}
		return jPasswordField;
	}
	
	public String GetUsuari(){
		return jTextFieldUsuari.getText();
	}
	
	public String GetContrasenya(){
		char[] contrasenya= jPasswordField.getPassword();
		return new String(contrasenya); 
	}
	
	public void TancarFinestra(){
		dispose();
	}

} 
